package com.lcc.lynx.base.system.controller;

import com.lcc.lynx.common.ResultVO;
import com.lcc.lynx.common.cache.GroupCache;
import com.lcc.lynx.common.cache.enums.CacheEnum;
import com.lcc.lynx.jdbc.IByIdController;
import com.lcc.lynx.security.annotation.Permission;
import com.lcc.lynx.base.system.pojo.dv.SysDictDV;
import com.lcc.lynx.base.system.pojo.dv.SysDictDtlDV;
import com.lcc.lynx.base.system.service.SysDictDtlService;
import com.lcc.lynx.base.system.service.SysDictService;
import com.lcc.lynx.common.utils.StrUtil;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * 字典明细控制实现
 * @auth lcc
 * @date 2025-01-01
 */
@RestController
@AllArgsConstructor
@Permission("sys:dict")
@RequestMapping("/sys/dict/dtl")
public class SysDictDtlController implements IByIdController<SysDictDtlDV> {

    private SysDictDtlService service;

    private SysDictService dictService;
    

    @Override
    public SysDictDtlService getService() {
       return service;
    }


    @PostMapping("/save")
    @Permission(":save")
    public ResultVO save(@RequestBody List<SysDictDtlDV> list) {
        if (StrUtil.isEmpty(list)) {
            return ResultVO.error("字典明细列表不能为空");
        }
        service.save(list);
        SysDictDV sysDictDV = dictService.selectById(list.get(0).getDictId());
        GroupCache.delVal(CacheEnum.dict + sysDictDV.getDictCode());
        return ResultVO.success();
    }

    @Override
    @Permission(":del")
    public ResultVO<?> delete(String idList) {
        if(StrUtil.isEmpty(idList)){
            return ResultVO.fail("idList不能为空");
        }
        List<String> list = Arrays.asList(idList.split(","));
        SysDictDtlDV sysDictDtlDV = service.selectById(list.get(0));
        SysDictDV sysDictDV = dictService.selectById(sysDictDtlDV.getDictId());
        GroupCache.delVal(CacheEnum.dict + sysDictDV.getDictCode());
        int i = getService().deleteByIdList(list);
        return i == 0 ? ResultVO.fail("删除失败") : ResultVO.success("成功");
    }
}